package org.jboss.as.quickstarts.poh5helloworld;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.log4j.Logger;

@Path("hw")
public class HelloWorld {

    final static Logger log = Logger.getLogger("lern.helloworld");

    @XmlRootElement
    public class EmptyClx {

        public String one = "one";
        public String two = "two";

        public EmptyClx(String a) {
            this.one = a;
        }

        public String getSome(String a) {
            return "some " + a;
        }
    }

    private String createHelloMsg(String name) {
        return "hello " + name + "!";
    }

    @POST
    @Path("json/{name}")
    @Produces("application/json")
    public String postHelloWorldJSON(@PathParam("name") String name) {
        log.info("name: " + name);
        return "{\"result\":\"" + createHelloMsg(name) + "\"}";
    }

    @POST
    @Path("xml/{name}")
    @Produces("application/xml")
    public String postHelloWorldXML(@PathParam("name") String name) {
        log.info("name: " + name);
        return "<xml><result>" + createHelloMsg(name)
                + "</result></xml>";
    }

    @GET
    @Path("xml/{name}")
    @Produces("application/xml")
    public String getHelloWorldXML(@PathParam("name") String name) {
        log.info("name: " + name);
        return "<xml><result>" + createHelloMsg(name)
                + "</result></xml>";
    }

    @GET
    @Path("json/{name}")
    @Produces("application/json")
    public String getHelloWorldJSON(@PathParam("name") String name) {
        log.info("name: " + name);
        return "{\"result\":\"" + createHelloMsg(name) + "\"}";
    }

    @GET
    @Path("obj/{a}")
    @Produces("application/json")
    public EmptyClx getObj(@PathParam("a") String a) {
        log.info("name: " + a);
        return new EmptyClx(a);
    }
}
